<template>
    <div>
        <el-menu default-active="1-4-1" class="el-menu-vertical-demo" @open="handleOpen" @close="handleClose"
            :collapse="menuIsCollapse" background-color="#545c64" text-color="#fff" active-text-color="#ffd04b">
            <!-- 标题 -->
            <h3>{{ menuIsCollapse ? '管理系统' : "报警管理系统" }}</h3>
            <!-- 不带子级的导航 -->
            <el-menu-item v-for="item in noChildren" :key="item.name" :index="item.name" @click="menuClick(item)">
                <i :class="`el-icon-${item.icon}`"></i>
                <span slot="title">{{ item.label }}</span>
            </el-menu-item>
            <!-- 带子级的导航 -->
            <el-submenu v-for="item in hasChildren" :key="item.label" :index="item.label">
                <template slot="title">
                    <i :class="`el-icon-${item.icon}`"></i>
                    <span>{{ item.label }}</span>
                </template>
                <!-- 子菜单 -->
                <el-menu-item-group>
                    <el-menu-item v-for="subItem in item.children" :key="subItem.name" :index="subItem.name"
                        @click="menuClick(subItem)">{{
                            subItem.label
                        }}</el-menu-item>
                </el-menu-item-group>
            </el-submenu>
        </el-menu>
    </div>
</template>

<script>
/*
    * @desc 容器左侧组件
    * @author rz
    * @date 2024-2-20
*/
import { mapState } from 'vuex';
import Cookie from 'js-cookie';
export default {
    data() {
        return {
        };
    },
    methods: {
        // 展开导航
        handleOpen(key, keyPath) {
            console.log("打印当前实例", this.$echarts);
            console.log(key, keyPath);
        },
        // 收缩导航
        handleClose(key, keyPath) {
            console.log(key, keyPath);
        },
        // 点击菜单
        menuClick(item) {
            this.$router.push(item.path).catch(err => {
                console.log('当前跳转的路由与之前的重复了');
            })
        }
    },
    computed: {
        ...mapState({
            // 菜单数据
            // menuData: state => state.tab.menu || Cookie.get('menuData'),
            // 菜单展开状态
            menuIsCollapse: state => state.tab.isCollapse,
        }),
        // 有子菜单的导航
        hasChildren() {
            return this.menuData.filter(item => item.children);
        },
        // 无子菜单的导航
        noChildren() {
            return this.menuData.filter(item => !item.children);
        },
        // 菜单数据
        menuData() {
            return this.$store.state.tab.menu.length ? this.$store.state.tab.menu : JSON.parse(Cookie.get('menuData'));
        }
    }
}
</script>

<style lang="less" scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 190px;
    min-height: 400px;
}

.el-menu {
    height: 100vh;
    border-right: none;

    h3 {
        font-size: 16px;
        text-align: center;
        color: white;
        line-height: 48px;

    }
}
</style>